from flask import Blueprint, jsonify, request

from app.cache.iron_redis import IronRedis
from app.controllers.response.response import SuccessResult
from app.database import convert
from app.exts import ironman_redis
from app.exts import mongo
from app.models.po.hanfan.hanfan_new_predict_po import HanfanNewPredictPO
from app.models.po.luzha.luzha_predict_po import LuzhaPredictPO
from app.services.business import gaolu_biz_service
from datetime import datetime
from app.services.business.hanfan import luzha_biz_service
from app.services.chart import chart_instance_dict
from app.services.dal import base_dao
from app.services.models import const
from app.services.models.hanfan_reason_model import hanfan_reason_model
from app.utils import date_util

bp = Blueprint('luzha_controller', __name__, url_prefix="/api/v1/luzha")
ping_bp = bp


@ping_bp.route('/prediction/info', methods=['GET'])
def luzha_prediction():
    result = {}

    luzha_prediction:LuzhaPredictPO = convert.query(LuzhaPredictPO,order_by=LuzhaPredictPO.DATE_TIME.desc()).first()
    if luzha_prediction is None:
        result['main'] = []
        result['recommend'] = []
        return SuccessResult(detail=result)
    hanfan_prediction: HanfanNewPredictPO = convert.query(HanfanNewPredictPO,
                                                          HanfanNewPredictPO.DATE_TIME == luzha_prediction.DATE_TIME
                                                          ).first()
    if hanfan_prediction is None:
        result['main'] = []
        result['recommend'] = []
        return SuccessResult(detail=result)

    result['main'] = []
    result['main'].append(dict(current=luzha_prediction.CG_LT_GL_GL04_Zha_R,
                               predict=luzha_prediction.CG_LT_GL_GL04_Zha_R_PREDICT,
                               name='碱度R2'))
    result['main'].append(dict(current=luzha_prediction.CG_LT_GL_GL04_Zha_Mg_Al,
                               predict=luzha_prediction.CG_LT_GL_GL04_Zha_Mg_Al_PREDICT,
                               name="Mg/Al"))
    result['main'].append(dict(current=luzha_prediction.CG_LT_GL_GL04_Zha_TiO2,
                               predict=luzha_prediction.CG_LT_GL_GL04_Zha_TiO2_PREDICT,
                               name="TiO2"))
    result['recommend'] = []
    keywords = [
        dict(key = "CG_LT_GL_GL04_Zha_TiO2",desc="炉渣二氧化钛",related="球团占比"),
        dict(key = "CG_LT_GL_GL04_Zha_R",desc="炉渣碱度",related="机烧占比"),
        dict(key = "CG_LT_GL_GL04_Zha_Mg_Al",desc="炉渣镁铝比",related="烧结矿占比"),
    ]

    for keyword in keywords:
        desc = keyword['desc']
        key = keyword['key']
        related = keyword['related']
        fact = luzha_prediction.__dict__.get(key)

        status = hanfan_reason_model.get_param_status(hanfan_prediction.CG_LT_GL_GL04_Yuanranliao_Ore_V2O5,
                                             key,
                                             fact)
        interval = hanfan_reason_model.get_param_interval(hanfan_prediction.CG_LT_GL_GL04_Yuanranliao_Ore_V2O5,
                                                          key,
                                                          fact)
        if status == const.NORMAL_CODE:
            recommend = "{}正常,建议保持此值".format(desc)
        elif status == const.LOW_CODE:
            recommend = "{}偏低,建议增加{},提高{}".format(desc,related,round(interval[0]-fact,2))

        else: # status == const.HIGH_CODE
            recommend = "{}偏高,建议降低{},降低{}".format(desc,related,round(fact-interval[1],2))

        result['recommend'].append(dict(key=key,desc=desc,status=status,recommend=recommend))


    return SuccessResult(detail=result)

@ping_bp.route("/chart", methods=['get'])
def chart_display():
    """
    抽象图表展示层,根据名字定位到具体业务
    :return:
    """
    name = request.args.get("name")
    start = request.args.get("start")
    end = request.args.get("end")

    chart_instance = chart_instance_dict[name]
    data = chart_instance.generate(start=start,end=end)

    return SuccessResult(detail=data)

